package com.ceba.test.task;

import com.ceba.base.helper.LoggerHelper;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.task.subtask.QrFileUploadTask;
import com.ceba.test.task.subtask.StorageFileBaseTask;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.task.subtask.CollectSignTimeTask;

public class MissionTasks {
	static Logger logger = LoggerFactory.getLogger(MissionTasks.class);
	 //创建一个SchedulerFactory工厂实例
    private static SchedulerFactory sf ;
    //通过SchedulerFactory构建Scheduler对象 
    private static Scheduler sche ;
    private final static String TRIGGER_GROUP_NAME = "QUARTZ_TRIGGERGROUP_NAME";
    private final static String SIGNCOUNT_JOB = "SIGNCOUNT_JOB";
    private final static String SIGNCOUNT_TRIGGER = "SIGNCOUNT_TRIGGER";

	//文件存储
	private final static String FILESTORAGE_JOB = "FILESTORAGE_JOB";
	private final static String FILESTORAGE_TRIGGER = "FILESTORAGE_TRIGGER";

	//二维码文件上传任务
	private final static String QR_FILE_UPLOAD_JOB = "QR_FILE_UPLOAD_JOB";
	private final static String QR_FILE_UPLOAD_TRIGGER = "QR_FILE_UPLOAD_TRIGGER";
    
    static{
    	getInstance();
    }
    public static Scheduler getInstance(){
    	if(sche == null){
    		try {
    			sf = new StdSchedulerFactory();
				sche= sf.getScheduler();
			} catch (SchedulerException e) {
				LoggerHelper.error(logger, e, "quartz init erro");
			}
    	}
    	return sche;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void addTask(String jobName,String triggerName,String cronSt,Class className) throws Exception{
    	 //创建定时任务
        JobDetail jobDetail = JobBuilder.newJob(className)
                .withIdentity(jobName, Scheduler.DEFAULT_GROUP)
                .build();
        //构建一个触发器，规定触发的规则
        //在任务调度器中，使用任务调度器的 CronScheduleBuilder 来生成一个具体的 CronTrigger 对象
        Trigger trigger = TriggerBuilder.newTrigger()
                            .withIdentity(triggerName, TRIGGER_GROUP_NAME)
                            .withSchedule(CronScheduleBuilder.cronSchedule(cronSt))
                            .build();//产生触发器
        //0 0 2 * * ? 代表每天的凌晨2点触发
		getInstance().scheduleJob(jobDetail,trigger);
		getInstance().start();
    }
    
    public static void run() {
    	try {
    		if(getInstance().isStarted()){
    			return;
    		}
    		//每天00:05触发
    		addTask(SIGNCOUNT_JOB,SIGNCOUNT_TRIGGER, "0 5 0 * * ?", CollectSignTimeTask.class);

    		//文件管理
			addTask(FILESTORAGE_JOB,FILESTORAGE_TRIGGER, "0 5 2 * * ?", StorageFileBaseTask.class);

			//二维码文件重传
			addTask(QR_FILE_UPLOAD_JOB,QR_FILE_UPLOAD_TRIGGER, CebaConfig.QR_FILE_UPLOAD_TIME, QrFileUploadTask.class);
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "task trigger fail");
		}
    }
    
    //停止
    public static void stop(){
    	try {
    		if(getInstance().isShutdown()){
    			return;
    		}
			getInstance().shutdown();
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "task stop fail");
		}
     }


}
